﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;


    public class DataFormat
    {

        #region  显示钱数
        /// <summary>
        /// 显示钱数
        /// </summary>
        /// <param name="c"></param>
        /// <returns></returns>
        public static string showMoney(decimal c)
        {
            return String.Format("{0:c}", c);
        }
        public static string showMoney(string c)
        {
            if (c == "") c = "0";
            return String.Format("{0:c}", Convert.ToDecimal(c));
        }
        #endregion

        #region 生成随机数
        /// <summary>
        /// 生成随机数
        /// </summary>
        /// <returns></returns>
        public static int GetRandomSeed()
        {
            byte[] bytes = new byte[4];
            System.Security.Cryptography.RNGCryptoServiceProvider rng = new System.Security.Cryptography.RNGCryptoServiceProvider();
            rng.GetBytes(bytes);
            return BitConverter.ToInt32(bytes, 0);
        }
        #endregion

        #region 剩余天数
        /// <summary>
        /// 格式化剩余天数
        /// </summary>
        /// <param name="planDT">截止日期</param>
        /// <returns>格式化的到截止日期的剩余天数</returns>
        public static string shengyuNtian_HTML(string planDT)
        {
            if (planDT != "")
            {
                string returnStr = "";
                double double_Ntian = shengyuNtian(Convert.ToDateTime(planDT));
                int Ntian = Convert.ToInt16(double_Ntian);
                if (Ntian > 3) returnStr += "还余<b>" + Ntian.ToString() + "</b>天";
                else if (Ntian < 0) returnStr += "<font color='#8E00FF'>超期<b>" + (Ntian * -1).ToString() + "</b>天</font>";
                else
                {
                    returnStr = "<font color=red>";
                    if (Ntian == 3) returnStr += "大后天";
                    if (Ntian == 2) returnStr += "后天";
                    if (Ntian == 1) returnStr += "明天";
                    if (Ntian == 0) returnStr += "<b>今天</b>";
                    returnStr += "</font>";
                }
                return returnStr;
            }
            else return "";
        }
        /// <summary>
        /// 计算剩余天数
        /// </summary>
        /// <param name="planDT">截止日期</param>
        /// <returns>到截止日期的剩余天数</returns>
        public static double shengyuNtian(DateTime planDT)
        {

            TimeSpan time = planDT.Subtract(DateTime.Now);
            return time.TotalDays;
        }
        #endregion

        #region 随机重组数组
        /// <summary>
        /// 随机重组数组
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="array"></param>
        /// <returns></returns>
        public static T[] RandomSort<T>(T[] array)
        {
            int len = array.Length;
            System.Collections.Generic.List<int> list = new System.Collections.Generic.List<int>();
            T[] ret = new T[len];
            Random rand = new Random();
            int i = 0;
            while (list.Count < len)
            {
                int iter = rand.Next(0, len);
                if (!list.Contains(iter))
                {
                    list.Add(iter);
                    ret[i] = array[iter];
                    i++;
                }

            }
            return ret;
        }
        #endregion

        

    }
    public class DateTime2YMD
    {
        public string Y;
        public string M;
        public string D;
        public DateTime2YMD(object DateTime_, string errorY_, string errorMonth_, string errorDay_)
        {
            try
            {
                DateTime v_ = Convert.ToDateTime(DateTime_);
                int y_ = v_.Year;
                int m_ = v_.Month;
                int d_ = v_.Day;
                int h_ = v_.Hour;
                int mm_ = v_.Minute;
                int s_ = v_.Second;
                Y = y_.ToString();
                M = m_.ToString();
                D = d_.ToString();
                if (h_ == 0) Y = errorY_;
                if (mm_ == 0) M = errorMonth_;
                if (s_ == 0) D = errorDay_;
            }
            catch (Exception e)
            {
                Y = errorY_;
                M = errorMonth_;
                D = errorDay_;
            }
        }
    }

